<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: RoleController.php 465 2012-02-12 05:33:23Z beimuaihui $
 */



class System_RoleController extends Baogg_Controller_Action {

	function init() {
		Zend_Loader::loadClass ( 'Role' );
		$this->role = new Role ( );

	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;
		
		$params=$this->getRequest()->getParams();
		$form_url=$params;
		
		Zend_Loader::loadClass("Baogg_View_Extgrid");
		
		$cm=array();
		$cm['role_id']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_PK) ;
		$cm['role_name']=array("sortable"=>true);
		$cm['user_group_id']=array("hidden"=>true);
		$cm['user_group_name']=array("sortable"=>false);
		
		//$cm['level_id']=array("hidden"=>true);
		//$cm['level_name']=array("sortable"=>false);		
		$cm['description']=array("sortable"=>false);
		$cm['ordering']=array("sortable"=>true);	
		$cm['is_default']=array("hidden"=>true);
		$cm['is_default2']=array("sortable"=>false);
		$cm['is_user_default']=array("hidden"=>true);
		$cm['is_user_default2']=array("sortable"=>false);
		$cm['state']=array("hidden"=>true);
		$cm['state2']=array("sortable"=>false);
		//$cm['action']=array("sortable"=>false);
		//$cm['ordering']=array("sortable"=>true);
		//$cm['sub_table']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_SUBTABLE) ; //Baogg_View_Extgrid::;
		
		$form_url['action']="data";
		$grid=new Baogg_View_Extgrid($form_url, $cm);
		$grid->setSelectionModel(true);
		$grid->setRowNumberer(true);
		
		/* $Resource=new Resource();
		$rs_subject=$Resource->getList(array('resource_code'=>'role'));
		$this->getRequest()->setParam('action_subject_id', $value);
		$url_params['subject_id']=@$rs_subject[0]['resource_id'];
		$rs_resource=$Resource->getList(array('resource_code'=>'menu'));
		$url_params['resource_id']=@$rs_resource[0]['resource_id']; */
		
		
		
		
		$grid->addExistButtons(array('add','remove','query'),array('edit','delete','role_permission'));
	//$grid->registerButton("fields","window.location.href='".BAOGG_BASE_URL."system/rolefield/list/';");
		
		$this->view->form_url=BAOGG_BASE_URL."system/role/save";
		$this->view->LANG=Zend_Json::encode($LANG);
		$this->view->grid = $grid->render();
		
		$Level=new Level();
		$arr_level=$Level->getCombo(array("state"=>1),array("sort"=>"ordering","dir"=>"asc"),null,array('level_id','level_name'));
		

		$UserGroup=new UserGroup();
		$arr_user_group = $UserGroup->getComboTree(array("state"=>1),null,null,array('user_group_id','user_group_name','user_group_pid'));
		array_unshift($arr_user_group,array("0",BAOGG_LANGUAGE::get('please_select')));
		//echo '<pre>';print_r($arr_user_group);exit;
		
		$form_url['action']='add';
		$form=new Baogg_View_Extform(array("url"=>$form_url,"height"=>300,"width"=>600));
		
		$form->addItem(0,array('name'=>'role_name'));
		$form->addItem(0,array('name'=>'user_group_id','xtype'=>'combo','store'=>$arr_user_group));
		//$form->addItem(0,array('name'=>'level_id','xtype'=>'combo','store'=>$arr_level));
		$form->addItem(1,array('name'=>'description'));
		$form->addItem(1,array('name'=>'ordering'));
		$form->addItem(2,array('name'=>'is_default','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));
		$form->addItem(2,array('name'=>'is_user_default','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_user_default")));
		$form->addItem(2,array('name'=>'state','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));
		$form->addItem(2,array('name'=>'role_id',"xtype"=>"hidden","hideLabel"=>true));
		$form->addExistButtons(array('-','save_grid','save_new','save','-','search','-','cancel'));//,'config','config'
		

		$this->view->form=$form;
		
		//echo '<pre>';var_dump($LANG);exit;


	}

	/**
	 * The data action
	 */
	function dataAction() {
		global $LANG;
	    $post=$this->_request->getPost ();
	    $where=array();$order=array();$limit=array();
	   foreach((array)$post as $k=>$v){
	       if($k=="start" || $k=="limit"){
	           $limit[$k]=$v;
	       }else if($k=="sort" || $k=="dir"){
	           $order[$k]=$v;
	       }else{
	           if($k=="role_id"  ){
	               if($v && is_numeric($v)){
	                   $v=(int)$v;
	                   $where[$k]=$v;
	               }
	           }else{
	               $v='%'.$v.'%';
	               $where[$k]=$v;
	           }
	           
	       }
	   }
	   if(!$order){
	   	$order=array("sort"=>"ordering","dir"=>"asc");
	   }
	   $rs=$this->role->getList($where,$order,$limit);
	   $rs_cnt=$this->role->getList($where,NULL,NULL,"count(*) as cnt");
	   $UserGroup=new UserGroup();
	   
	  /*  $Level=new Level();
	   $rs_level=$Level->getList(array("state"=>1));
	   $arr_level=array();
	   foreach((array)$rs_level as $v){
	   		$arr_level[$v['level_id']]=$v['level_name'];
	   } */
	   $arr_user_group=array();
	   $arr_user_default=Baogg_Language::get('arr_user_default');
	   $arr_state=Baogg_Language::get('arr_state');
	   foreach((array)$rs as $k=>$v){
	   	    if(!isset($arr_user_group[$v['user_group_id']])){
	   	    	$rs_user_group=$UserGroup->getOne($v['user_group_id']);
	   	    	$arr_user_group[$v['user_group_id']]=@$rs_user_group[0]['user_group_name'];
	   	    }
	   		$rs[$k]['user_group_name']=$arr_user_group[$v['user_group_id']];
	   		$rs[$k]['is_default2']=$arr_state[$v['is_default']];
	   		$rs[$k]['state2']=$arr_state[$v['state']];
	   		$rs[$k]['is_user_default2']=$arr_user_default[$v['is_user_default']];
        	//$rs[$k]['level_name']=@$arr_level[$v['level_id']];
        	
        	/*$js_win="(function(){(new Ext.ux.Dialog({id:'resource_role_win',url:'".BAOGG_BASE_URL."system/resource/role/role_id/{$v['role_id']}',width:800,height:500,modal:true})).show();return ;})();";
        	$rs[$k]['action']='<a href="javascript:'.$js_win.'"><img src="'.BAOGG_THEME.'images/action/edit.png" title="'.$LANG['role_id'].'" /></a>';*/
        }
		$this->view->grid = Zend_Json::encode(array("count"=>$rs_cnt[0]['cnt'],"data"=>$rs));
	}

	
	/**
	 * The add action
	 */
	function addAction(){
		$this->saveAction();
	}
	
	/**
	 * The edit action
	 */
	function editAction() {
		$this->saveAction();
	}


	/**
	 * The delete action
	 */
	function deleteAction() {
        
	    $ret=array("success"=>false,"msg"=>"delete_fail");
	    $id = Baogg_Controller_Action::_getParam ( 'ids' );
		 
		   
		
		if ($this->role->_delete ( $id )){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}
		
		echo Baogg_Language::outputResult($ret);
		$this->_helper->viewRenderer->setNoRender ();
	}
	
	function removeAction(){
		$this->deleteAction();
	}

	/**
	 * The save action
	 */
	function saveAction() {
		//global $LANG;
		
	    $ret=array("success"=>false,"msg"=>"save_fail");
	    
	    //check form
		if (!$this->_request->isPost ())
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;
		}
		
		$formData = $this->_request->getPost ();

		if(!$formData)
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;     
		}
			
		$arr=$this->role->filterForm($formData);
        $action=$this->getRequest()->getParam('action');
		$rs = $this->role->getSame($arr);
		if($action=='edit'){
			if(count($rs)==0 || count($rs)==1 &&  $rs[0] == $arr["role_id"] ){
			    $this->role->_update($arr,$arr["role_id"]);
			    $ret['success'] = true;				
				$ret['msg'] = "save_ok";			
			}else{
				$ret['msg'] = "exist_same_record";
			}
			
		}else{
			//insert record
			//$permission_type = 1;			
			if($rs){				
				$ret['msg'] = "exist_same_record";
			}else{
			    unset($arr["role_id"]);
				$ret['success'] = $this->role->_insert($arr)>0?true:false;
				$ret['msg'] = "add_ok";
			}
		}
				
		echo Baogg_Language::outputResult($ret);
		
		$this->_helper->viewRenderer->setNoRender ();
		
		
	}

}
